package com.itheima.dao.impl;


import com.itheima.dao.AccountDao;
import com.itheima.dao.ScoreDao;
import com.itheima.pojo.Account;
import com.itheima.pojo.Score;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public class ScoreDaoImpl implements ScoreDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public Score findById(int id) {
        String sql="select * from score where id=?";
        List<Score> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Score.class), id);
        if (list!=null&&list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public void update(Score score) {
        String sql="update score set score=score+? where id=?";
        jdbcTemplate.update(sql,score.getScore(),score.getId());
    }

    @Override
    public void save(Score score) {
        String sql="insert into score values(?,?)";
        jdbcTemplate.update(sql,score.getId(),score.getScore());
    }
}
